﻿#include "algoform.h"
#include "ui_algoform.h"
#include <QVBoxLayout>
#include <qdebug.h>
#include "algosetform.h"
#include "singleton.h"
#include "myapp.h"

AlgoForm::AlgoForm(QString devType, QWidget* parent) :
    QWidget(parent),
    ui(new Ui::AlgoForm)
{
    m_devType = devType;
    ui->setupUi(this);
    this->initForm();
}

AlgoForm::~AlgoForm()
{
    delete ui;
}
void AlgoForm::initForm()
{
    QStringList header;
    header << "通道ID" << "事件类型" << "报警类型" << "IPC 地址" << "IP 用户名" << "IPC 密码" << "实时流地址" << "通道使能" << "通道状态" << "操作";
    ui->tableWidget->setColumnCount(header.size());
    ui->tableWidget->setHorizontalHeaderLabels(header);
    for (int i = 0; i < header.size(); i++)
    {
        ui->tableWidget->setColumnWidth(i, 120);
    }
    ui->tableWidget->setColumnWidth(6, 180);
    ui->tableWidget->setColumnWidth(7, 100);
    ui->tableWidget->setColumnWidth(8, 100);
    ui->tableWidget->setColumnWidth(9, 100);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    //  ui->tableWidget->setAlternatingRowColors(true);
    ui->tableWidget->verticalHeader()->setHidden(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);


#if TEST
    for (int i = 0; i < 2; i++)
    {

        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setRowHeight(row, 30);
        QHBoxLayout* hlayout = new QHBoxLayout();
        hlayout->addStretch();
        QPushButton* modify = new QPushButton(this);
        QPushButton* remove = new QPushButton( this);

        connect(modify, &QPushButton::clicked, this, &AlgoForm::modifyBtnClicked);

        connect(remove, &QPushButton::clicked, this, &AlgoForm::removeBtnClicked);
        modify->setStyleSheet("QPushButton{border-image: url(:/image/bianji.png);width:20px;height:22px;} "
                              "QPushButton:hover {border-image: url(:/image/bianji_hover.png);}");
        remove->setStyleSheet("QPushButton{border-image: url(:/image/shanchu.png);width:20px;height:22px;} "
                              "QPushButton:hover {border-image: url(:/image/shanchu_hover.png);}");

        hlayout->addWidget(modify);
        hlayout->addWidget(remove);
        hlayout->addStretch();
        QWidget* btns = new QWidget(this);
        btns->setLayout(hlayout);
        ui->tableWidget->setCellWidget(row, 9, btns);
        ui->tableWidget->setItem(row, 0, new QTableWidgetItem("1"));
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem("1"));
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem("123"));
        ui->tableWidget->setItem(row, 3, new QTableWidgetItem("123"));
        ui->tableWidget->setItem(row, 4, new QTableWidgetItem("123"));
        ui->tableWidget->setItem(row, 5, new QTableWidgetItem("0"));

    }
#endif



}
void AlgoForm::modifyBtnClicked()
{

    QPushButton* Obj = qobject_cast<QPushButton*>(this->sender());
    if (Obj == nullptr)
    {
        return;
    }
    QModelIndex qIndex = ui->tableWidget->indexAt(QPoint(Obj->parentWidget()->frameGeometry().x(), Obj->parentWidget()->frameGeometry().y()));
    int row = qIndex.row();
    int column = qIndex.column();

    QString chnid = ui->tableWidget->item(row, 0)->text();
    AlgoSetForm* algosetform = new AlgoSetForm(m_devType, chnid, NULL);
    algosetform->setObjectName("algosetform");


//    algosetform->setWindowModality(Qt::ApplicationModal);
    algosetform->show();
    //algosetform->move((this->width() - algosetform->width()) / 2, (this->height() - algosetform->height()) / 2);
    connect(Singleton::Instance(), &Singleton::sigAlarmImage, algosetform, &AlgoSetForm::onAlarmImage);
    connect(Singleton::Instance(), &Singleton::sigGetZoneParam, algosetform, &AlgoSetForm::onGetZoneParam);
#if 0
    connect(Singleton::Instance(), &Singleton::sigGetAlgoParam, algosetform, &AlgoSetForm::onGetAlgoParam);
    connect(Singleton::Instance(), &Singleton::sigGetAlarmOut, algosetform, &AlgoSetForm::onGetAlarmOut);
    connect(Singleton::Instance(), &Singleton::sigGetExternalParam, algosetform, &AlgoSetForm::onGetExternalParam);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoPeopleCountParam, algosetform, &AlgoSetForm::onGetAlgoPeopleCountParam);
    algosetform->on_btn_getparam_1_clicked();
    algosetform->on_btn_getparam_2_clicked();
    algosetform->on_btn_getparam_3_clicked();
#endif
}
void AlgoForm::removeBtnClicked()
{
    QPushButton* Obj = qobject_cast<QPushButton*>(this->sender());
    if (Obj == nullptr)
    {
        return;
    }
    QModelIndex qIndex = ui->tableWidget->indexAt(QPoint(Obj->parentWidget()->frameGeometry().x(), Obj->parentWidget()->frameGeometry().y()));

    int row = qIndex.row();
    int column = qIndex.column();
    ui->tableWidget->removeRow(row);
    //将该通道的算法删除

}



void AlgoForm::onChnInfo(QJsonObject obj )
{
    QJsonArray chnInfo = obj["chnInfo"].toArray();
    for(int i = 0; i < chnInfo.size(); i++)
    {

        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setRowHeight(row, 24);
        QHBoxLayout* hlayout = new QHBoxLayout();
        hlayout->addStretch();
        QPushButton* modify = new QPushButton(this);
        QPushButton* remove = new QPushButton( this);

        connect(modify, &QPushButton::clicked, this, &AlgoForm::modifyBtnClicked);

        connect(remove, &QPushButton::clicked, this, &AlgoForm::removeBtnClicked);
        modify->setStyleSheet("QPushButton{border-image: url(:/image/bianji.png);width:18px;height:18px;} "
                              "QPushButton:hover {border-image: url(:/image/bianji_hover.png);}");
        remove->setStyleSheet("QPushButton{border-image: url(:/image/shanchu.png);width:20px;height:20px;} "
                              "QPushButton:hover {border-image: url(:/image/shanchu_hover.png);}");

        hlayout->addWidget(modify);
        hlayout->setMargin(0);
        //hlayout->addWidget(remove);
        hlayout->addStretch();
        QWidget* btns = new QWidget(this);
        btns->setLayout(hlayout);
        ui->tableWidget->setCellWidget(row, 9, btns);
        ui->tableWidget->setItem(row, 0, new QTableWidgetItem(chnInfo.at(i).toObject().value("chnId").toString()));
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem(chnInfo.at(i).toObject().value("eventType").toString()));
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem(chnInfo.at(i).toObject().value("alarmType").toString()));
        ui->tableWidget->setItem(row, 3, new QTableWidgetItem(chnInfo.at(i).toObject().value("ipcIp").toString() ));
        ui->tableWidget->setItem(row, 4, new QTableWidgetItem(chnInfo.at(i).toObject().value("ipcUser").toString()));
        ui->tableWidget->setItem(row, 5, new QTableWidgetItem(chnInfo.at(i).toObject().value("ipcPassword").toString()));
        ui->tableWidget->setItem(row, 6, new QTableWidgetItem(chnInfo.at(i).toObject().value("streamUrl").toString()));
        ui->tableWidget->setItem(row, 7, new QTableWidgetItem(QString::number(chnInfo.at(i).toObject().value("enable").toInt())));
        ui->tableWidget->setItem(row, 8, new QTableWidgetItem(QString::number(chnInfo.at(i).toObject().value("online").toInt())));

    }
}

